package com.light.gulimall.cart.controller;

import com.light.gulimall.cart.interceptor.CartInterceptor;
import com.light.gulimall.cart.service.CartService;
import com.light.gulimall.cart.vo.Cart;
import com.light.gulimall.cart.vo.CartItem;
import com.light.gulimall.cart.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCartListPage();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 得到用户当前选中的购物车商品信息
     * @return
     */
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems() {
        try {
            List<CartItem> currentUserCartItems = cartService.getCurrentUserCartItems();
            return currentUserCartItems;
        } catch (Exception e) {
            log.error("getCurrentUserCartItems错误，原因：",e);
            return null;
        }
    }

    /**
     *  添加商品到购物车
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) {
        try {
            cartService.addToCart(skuId, num);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //携带skuId重定向到addToCartSuccess页面
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess";
    }

    /**
     * 跳转到购物车成功页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItem cartItem = cartService.getInfoBySkuId(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("prop") Boolean prop) {
        cartService.checkItem(skuId,prop);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/modifyNum")
    public String modifyNum(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.modifyNum(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
}
